package logic

import (
	"3kz/models/mysql"
	"3kz/zk3rpc/internal/svc"
	"3kz/zk3rpc/zk3rpc"
	"context"
	"fmt"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddLogic {
	return &AddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddLogic) Add(in *zk3rpc.AddRequest) (*zk3rpc.AddResponse, error) {
	// todo: add your logic here and delete this line

	Add := mysql.Produce{
		ProductCode:  in.ProductCode,
		ProductName:  in.ProductName,
		ProductPrice: in.ProductPrice,
		ProductCost:  in.ProductCost,
		Img:          in.Img,
		UserId:       int(in.UserId),
	}

	mysql.DB.Debug().Model(&Add).Create(&Add)

	if Add.ID == 0 {
		return nil, fmt.Errorf("商品已经存在,请重新输入")
	}

	return &zk3rpc.AddResponse{Id: in.UserId}, nil
}
